﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using Model;
using System.Data;

namespace gridview书籍管理
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private int currentid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
                //排序表达式  实际上就是 字段名（ViewState作为全局变量）
                ViewState["sortexpression"] = "Id";
                //排序顺序
                ViewState["sortorder"] = "ASC";
                //绑定数据
                Bind();
            }
        }
        public void Bind()
        {
           // this.gridview1.DataSource = BLL.UserBLL.selectAllbooks();
            //设置datakey
            this.gridview1.DataKeyNames = new string[] { "Id" };
            //使用dataview进行排序
            DataView view = BLL.UserBLL.selectAllbooks().DefaultView;
            //设置排序
            view.Sort = ViewState["sortexpression"] + " " + ViewState["sortorder"];
            //设置dataview
            this.gridview1.DataSource = view;
            //绑定
            this.gridview1.DataBind();
        }
        /// 分页时
        protected void gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ///分页
            this.gridview1.PageIndex = e.NewPageIndex;
            //把编辑行恢复原状
            this.gridview1.EditIndex = -1;
            //重新绑定
            Bind();
        }
        //行编辑时
        protected void gridview1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //设置编辑行
            this.gridview1.EditIndex = e.NewEditIndex;
            //保存当前正在编辑的行的id
            currentid = Convert.ToInt32(this.gridview1.DataKeys[e.NewEditIndex].Value);
            //重新绑定
            Bind();
            
        }
        //退出编辑
        protected void gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //编辑行恢复
            this.gridview1.EditIndex = -1;
            Bind();
        }
        //行数据绑定
        protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (this.gridview1.EditIndex == e.Row.RowIndex) //当前绑定数据行是编辑行
            {
                int currentindex = e.Row.RowIndex;
                DropDownList ddl =(DropDownList) (e.Row.FindControl("ddEditType"));
                if (ddl != null)
                {
                   bookinfo bi= UserBLL.selectbook(currentid);
                    if (bi.Booktype == "其它")
                    {
                        ddl.SelectedIndex = 1;
                    }
                    else {
                        ddl.SelectedIndex = 0;
                    }

                }
            }
        }
        //行更新时
        protected void gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           int index= e.RowIndex;
            TextBox txtTitle=   this.gridview1.Rows[index].FindControl("txtEditTitle") as TextBox;
            TextBox txtDes = this.gridview1.Rows[index].FindControl("txtEditDes") as TextBox;
            DropDownList ddl = this.gridview1.Rows[index].FindControl("ddEditType") as DropDownList;
            TextBox txtPrice = this.gridview1.Rows[index].FindControl("txtEditPrice") as TextBox;
            /////
            bookinfo bi = new bookinfo();
            bi.Title = txtTitle.Text;
            bi.Decsript = txtDes.Text;
            bi.Booktype = ddl.SelectedValue;
            bi.Price = Convert.ToDouble(txtPrice.Text.Trim());
            ///获取id
            bi.Id = Convert.ToInt32(this.gridview1.DataKeys[index].Value);
            BLL.UserBLL.updateBook(bi);
            ////关闭编辑行
            this.gridview1.EditIndex = -1;
            //重新绑定数据
            Bind();
        }
        //行删除时
        protected void gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index=e.RowIndex;
            int id=Convert.ToInt32( this.gridview1.DataKeys[index].Value);
            BLL.UserBLL.deleteByid(id);
            //重新绑定
            Bind();
        }
        //脚注里面的添加数据按钮点击事件
        protected void btnadd_Click(object sender, EventArgs e)
        {
            //行对象.FindControl("ID") 
            // FooterRow.FindControl()
            TextBox txttitle=this.gridview1.FooterRow.FindControl("txtTitle") as TextBox;
            TextBox txtDes = this.gridview1.FooterRow.FindControl("txtDes") as TextBox;
            DropDownList ddl = this.gridview1.FooterRow.FindControl("ddType") as DropDownList;
            TextBox txtPrice = this.gridview1.FooterRow.FindControl("txtPrice") as TextBox;
            ////
            bookinfo bi = new bookinfo();
            bi.Title = txttitle.Text;
            bi.Decsript = txtDes.Text;
            bi.Booktype = ddl.SelectedValue;
            bi.Price = Convert.ToDouble(txtPrice.Text.Trim());
            //
            BLL.UserBLL.addBook(bi);
            Bind();
        }
        //全选
        protected void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkAll.Checked == true)
            {

                for (int i = 0; i < this.gridview1.Rows.Count; i++)
                {
                    CheckBox chb = this.gridview1.Rows[i].FindControl("chkbox") as CheckBox;
                    chb.Checked = true;
                }
            }
            else {
                for (int i = 0; i < this.gridview1.Rows.Count; i++)
                {
                    CheckBox chb = this.gridview1.Rows[i].FindControl("chkbox") as CheckBox;
                    chb.Checked = false;
                }
            }
        }
        //选择删除按钮
        protected void btndelall_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.gridview1.Rows.Count; i++)
            {
                CheckBox chb = this.gridview1.Rows[i].FindControl("chkbox") as CheckBox;
                if (chb.Checked == true)
                {
                    int id = Convert.ToInt32(this.gridview1.DataKeys[i].Value);
                    BLL.UserBLL.deleteByid(id);
                }
            }
            Bind();
        }
        //求和
        protected void btnpricesum_Click(object sender, EventArgs e)
        {
            double sum = 0;
            for (int i = 0; i < this.gridview1.Rows.Count; i++)
            {
                CheckBox chb = this.gridview1.Rows[i].FindControl("chkbox") as CheckBox;
                if (chb.Checked == true)
                {
                    Label lab=this.gridview1.Rows[i].FindControl("labPrice") as Label;
                    sum += Convert.ToDouble(lab.Text.Trim());
                }
            }
            this.labsum.Text = sum.ToString();
        }
        //排序时
        protected void gridview1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //产生点击事件的排序字符串
            string expression = e.SortExpression;
            if (ViewState["sortexpression"].ToString() == expression)  //如果上次点击的是这个，就直接设置ASC,DESC
            {
                if (ViewState["sortorder"].ToString() == "ASC")
                {
                    ViewState["sortorder"] = "DESC";
                }
                else
                {
                    ViewState["sortorder"] = "ASC";
                }
            }
            else
            {
                //如果上次没有点击这个 就直接排序
                ViewState["sortexpression"] = expression;
            }
            /////////////这里只是设置一些全局变量 实际排序操作  在 DataView的Sort排序字符串里面设置
            Bind();//重新绑定
        }
    }
}